/**
 * 倒计时
 */

import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { TimeTools } from "../common/TimeTools";
import { AudioManager1 } from "../manager/AudioManager1";
import { EventManager } from "../manager/EventManager";
import { LevelManager } from "../manager/LevelManager";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;
enum TIME_STATE_ENUM {
    INIT,/** 初始状态 */
    START,/** 倒计时中 */
    PAUSE,/** 暂停 */
    END,/** 倒计时结束 */
}
@ccclass
export default class TimeItem extends cc.Component {
    @property(cc.Label)
    time: cc.Label = null;

    totalTime: number = 0;// 总时间
    timer: number = 0;// 时间
    updateTimer: number = 1;// 1秒刷新一次
    state: TIME_STATE_ENUM;
    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.ADD_TIME, this.addTime, this);
        EventManager.ins.on(EVENT_NAME_ENUM.START_TIME, this.startTime, this);
        EventManager.ins.on(EVENT_NAME_ENUM.PAUSE_TIME, this.pauseTime, this);
        EventManager.ins.on(EVENT_NAME_ENUM.RESUME_TIME, this.resumeTime, this);
        EventManager.ins.on(EVENT_NAME_ENUM.STOP_TIME, this.stopTime, this);
        EventManager.ins.on(EVENT_NAME_ENUM.AGAIN_GAME_RESUME_TIME, this.againGameTime, this);
        this.state = TIME_STATE_ENUM.INIT;
    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.ADD_TIME, this.addTime, this);
        EventManager.ins.off(EVENT_NAME_ENUM.START_TIME, this.startTime, this);
        EventManager.ins.off(EVENT_NAME_ENUM.PAUSE_TIME, this.pauseTime, this);
        EventManager.ins.off(EVENT_NAME_ENUM.RESUME_TIME, this.resumeTime, this);
        EventManager.ins.off(EVENT_NAME_ENUM.STOP_TIME, this.stopTime, this);
        EventManager.ins.off(EVENT_NAME_ENUM.AGAIN_GAME_RESUME_TIME, this.againGameTime, this);
    }
    initTime(time: number) {
        this.timer = time;
        this.totalTime = time;
        this.updateTimeLabel();
    }

    /** 刷新时间UI */
    updateTimeLabel() {
        this.time.string = TimeTools._ins.secondToMS(this.timer);
        EventManager.ins.emit(EVENT_NAME_ENUM.UPDATE_SLIDER, this.timer / this.totalTime);
    }


    update(dt: number) {
        if (this.state != TIME_STATE_ENUM.START) return;

        this.updateTimer -= dt;
        if (this.updateTimer <= 0) {
            this.updateTimer = 1;
            this.timer -= 1;
            if (this.timer <= 0) {
                this.timer = 0;
                this.pauseTime();

                let lvName = LevelManager.ins.getBundleName();
                if (lvName == "15_268") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_268")
                    level15.pauseAni();
                }
                if (lvName == "15_271") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_271")
                    level15.pauseAni();
                }
                if (lvName == "15_272") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_272")
                    level15.pauseAni();
                }
                if (lvName == "15_279") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_279")
                    level15.pauseAni();
                }
                if (lvName == "15_281") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_281")
                    level15.pauseAni();
                }
                if (lvName == "15_287") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_287")
                    level15.pauseAni();
                }
                if (lvName == "15_286") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_286")
                    level15.pauseAni();
                }
                if (lvName == "15_288") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_288")
                    level15.pauseAni();
                }
                if (lvName == "15_289") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_289")
                    level15.pauseAni();
                }
                if (lvName == "15_299") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_299")
                    level15.pauseAni();
                }
                if (lvName == "15_301") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_301")
                    level15.pauseAni();
                }
                if (lvName == "15_302") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_302")
                    level15.pauseAni();
                }
                if (lvName == "15_304") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_304")
                    level15.pauseAni();
                }
                if (lvName == "15_305") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_305")
                    level15.pauseAni();
                }
                if (lvName == "15_308") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_308")
                    level15.pauseAni();
                }
                if (lvName == "15_307") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_307")
                    level15.pauseAni();
                }
                if (lvName == "15_290") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_290")
                    level15.pauseAni();
                }
                if (lvName == "15_309") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_309")
                    level15.pauseAni();
                }
                if (lvName == "15_317") {
                    let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_317")
                    level15.pauseAni();
                }
                UIManager.ins.showPop("TimeUI", BUNDLE_TYPE_ENUM.GAME);
                AudioManager1.stopAudioForName("daojishi");
            }
            if (this.timer === 4 || this.timer === 2) {
                AudioManager1.playAudio("daojishi", 1, BUNDLE_TYPE_ENUM.GAME);
            }
            this.updateTimeLabel();
        }
    }
    /** 加时 */
    addTime(num = 60) {
        this.timer += num;
        // if (this.timer > this.totalTime) this.totalTime = this.timer;
        this.totalTime += num;
        if (this.timer >= 3600) this.timer = 3600;
        if (this.totalTime >= 3600) this.totalTime = 3600;
        this.updateTimeLabel();
        this.resumeTime();
    }
    /** 初始开始计时 */
    startTime() {
        if (this.state == TIME_STATE_ENUM.INIT)
            this.state = TIME_STATE_ENUM.START;
    }
    /** 暂停计时 */
    pauseTime() {
        if (this.state == TIME_STATE_ENUM.START) {
            this.state = TIME_STATE_ENUM.PAUSE;
            AudioManager1.stopAudioForName("daojishi");
        }
    }
    /** 暂停后继续计时 */
    resumeTime() {
        if (this.state == TIME_STATE_ENUM.PAUSE) {
            this.state = TIME_STATE_ENUM.START;
        }
    }
    /** 停止计时 */
    stopTime() {
        this.state = TIME_STATE_ENUM.END;
        AudioManager1.stopAudioForName("daojishi");
    }
    /**继续开始计时 */
    againGameTime() {
        if (this.state == TIME_STATE_ENUM.END) {
            this.state = TIME_STATE_ENUM.START;
        }
    }
}